//
//  ZCRecommendTableViewController.m
//  百思不得姐
//
//  Created by Ning Xie on 16/8/25.
//  Copyright © 2016年 zhangchong. All rights reserved.
//

#import "ZCRecommendTableViewController.h"
#import "ZCRecommendModel.h"
#import "ZCRecommendCell.h"

@interface ZCRecommendTableViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic)NSArray *recommendArray;
@end

@implementation ZCRecommendTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self loadData];

    
}

-(NSArray *)recommendArray{
    if (_recommendArray == nil) {
        
        _recommendArray = [NSArray array];
    }
    return _recommendArray;
}
/**
 *  页面初始化
 */
-(void)setupUI{
    
    self.navigationItem.title = @"推荐订阅";
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = ZCGlobalRBG;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

}
/**
 *  加载数据
 */
-(void)loadData{
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    [[ZCNetworkingTools sharedNetWorkingTools] requestRecommend:^(NSDictionary *response, NSError *error) {
        
        if (error != nil) {
            ZCLog(@"数据请求错误 %@",error);
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
            return ;
        }
        if (![response isKindOfClass:[NSArray class]]) {
            ZCLog(@"不是一个合法的数组");
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
            return;
        }
        [SVProgressHUD dismiss];
        
        self.recommendArray = [ZCRecommendModel mj_objectArrayWithKeyValuesArray:response];
        
        [self.tableView reloadData];

        
    }];
}


//MARK:TableView数据源方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.recommendArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZCRecommendCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RecommendCell"];
    
    if (cell == nil) {
        cell = [[ZCRecommendCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RecommendCell"];
    }
    
    cell.model = self.recommendArray[indexPath.row];
    return cell;
}




@end
